
import { Asset } from "cc";
import { EventMgr } from "../manager/EventMgr";
import { ResCacheKey } from "../manager/ResourceMgr";

export default class BaseMgr {

    private resArray: ResCacheKey[] = []
    private eventList: Array<Map<string, Function>> = [];
    protected isDestroy: boolean = false;
    public constructor() { }
    /*
    * 添加监听的事件
    * @param listenename 事件名
    * @param fun 响应的方法
    */
    protected addEvent(listenename: string, fun: Function) {
        EventMgr.getInstance().on(listenename, this, fun);
        let map: Map<string, Function> = new Map();
        map.set(listenename, fun);
        this.eventList.push(map);
    }
    /**
     * 发送事件
     * @param listenerName 事件名称
     * @param param1 
     * @param param2 
     * @param param3 
     * @param param4 
     * @param param5 
     */
    protected sendEvent(listenerName: string, param1?: any, param2?: any, param3?: any, param4?: any, param5?: any) {
        EventMgr.getInstance().emit(listenerName, param1, param2, param3, param4, param5);
    }




    /**
     * 添加到数组
     * @param bundlePath 
     * @param type 
     */
    public addRes(bundlePath: string, type: typeof Asset) {
        // let obj = new ResCache(bundlePath, 0, 0, type);
        // this.resArray.push(obj)
    }

    private release() {

    }
    setListenerUI() { }

    //去除监听的事件
    offEvents() {
        if (this.eventList && this.eventList.length > 0) {
            for (let i = 0; i < this.eventList.length; i++) {
                let event = this.eventList[i]
                event.forEach((fun: Function, name: string) => {
                    EventMgr.getInstance().off(name, this, fun);
                })

            }
            this.eventList = null;
        }
    }


    onDestroy() {
        this.isDestroy = true;
        this.release();
    }


}
